home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / science / earthqua.zip / EQSIM / EQSIM.CPP next >
C/C++ Source or Header  |  1995-02-04  |  20KB  |  901 lines

  1. // EQSIM.CPP - The simulator for the earthquake damage prevention program.
  2. // Generates the building movement and displays it.
  3. // Created by Misha Koshelev.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <conio.h>
  9. #include <dos.h>
  10. #include <string.h>
  11. #include <alloc.h>
  12. #include <graphics.h>
  13. #include "svga256.h"
  14. #include "gendisp.h"
  15.  
  16. // Misc. definitions for init_globals
  17. #define WAVELET_NUM 15
  18. #define DELTA_OMEGA 0.2
  19. #define FRACTAL_DIM 0.35
  20.  
  21. // Definitions for active and hybrid control
  22. #define DELAY 3
  23. #define CONTROL_NORMAL 2.0
  24. #define HYBRID_SPOINT 1.0
  25.  
  26. // Program defaults
  27. #define FELASTIC 1
  28. #define FFRICTION 1
  29. #define TSTEP 0.1
  30. #define SNUM 300
  31. #define BMASS 10.00
  32. #define MAGNIFICATION 1.0
  33.  
  34. // Some defines
  35. #define EXIT 255.255
  36.  
  37. typedef double (* cntrlfunc)(double);            // The definition for the
  38.                                                // control function.
  39. typedef struct
  40. {
  41.     double f, a, v, x, e;                    // The force, accelaration,
  42.                         // veloctity, energy used,
  43.                         // and x movement of a
  44.                         // building
  45.                         // where the original position
  46.                         // is 0.0
  47. } bms;
  48.  
  49. bms far *blmov = NULL;                          // The building movement
  50.                                                // array.
  51. double felastic_cos, ffriction_cos;             // The coefficients for
  52.                                                // elastic and friction
  53.                                                // forces.
  54. double tstep;                                                       // The time step used for
  55.                                                // the building movement.
  56. int   snum;                                                                    // The number of steps.
  57. double bmass;                                                               // The mass of the building.
  58.  
  59. // Predefined headers
  60. int huge Svga256Detect(void);
  61. double no_cntrl(double);
  62. double active_cntrl(double);
  63. double hybrid_cntrl(double);
  64. double execute_cntrl(cntrlfunc, double);
  65. void init_qglobals(double, double, double, double, int, double);
  66. void init_gen_qglobals(double, double, double, int, double);
  67. void done_qglobals(void);
  68. void done_gen_qglobals(void);
  69. void gen_blmov(cntrlfunc);
  70. void plot_blmov(int, int, int, int, int, int, int);
  71. void plot_disp(int, int, int, int, int, int, int);
  72. void loadpal(char *);
  73. void hrainbow(int, int, int, int, int, int, int, int);
  74. void vrainbow(int, int, int, int, int, int, int, int);
  75. double total_blmov(void);
  76. double max_blmov(void);
  77. double total_eused(void);
  78. double show_control(cntrlfunc, char *, double);
  79. void show_control_txt(cntrlfunc, char *, double);
  80. double select_intensity(void);
  81.  
  82. // Global variables
  83. int bpoly[10];
  84.  
  85. // Fill pattern, to fill the building with
  86. char bpattern[8] = {0xFF, 0xFF, 195, 195, 195, 195, 0xFF, 0xFF};
  87.  
  88. // Set if program should write the result to a file (default)
  89. int wtof = TRUE;
  90.  
  91. FILE far *tmf, far *mmf, far *ef;
  92.  
  93. // Set if demo mode (default off)
  94. int demomode = FALSE;
  95.  
  96. // Set if no graphics (default off)
  97. int graphics = TRUE;
  98.  
  99. // The main program.
  100. //
  101. #ifdef STANDALONE
  102. void main(int argc, char *argv[])
  103. #else
  104. void eqsimmain(void)
  105. #endif
  106. {
  107.    double ti, i, intensity;
  108.    int ai;
  109.    int argprocessed = FALSE;
  110.    int gd = DETECT, gm, errorcode;
  111.  
  112. #ifdef STANDALONE
  113.    // Check for parameters
  114.    if (argc > 0)
  115.    {
  116.       for (ai = 1; ai < argc; ai++)
  117.       {
  118.      // If parameter is disable write to file, disable it
  119.      if (!strcmp(argv[ai], "/NOOUTPUTFILES"))
  120.      {
  121.         argprocessed = TRUE;
  122.         wtof = FALSE;
  123.      }
  124.      // If parameter is demo mode, enable it
  125.      if (!strcmp(argv[ai], "/DEMO"))
  126.      {
  127.         argprocessed = TRUE;
  128.         demomode = TRUE;
  129.      }
  130.      // If parameter is no graphics, disable it
  131.      if (!strcmp(argv[ai], "/NOGRAPHICS"))
  132.      {
  133.         argprocessed = TRUE;
  134.         wtof = TRUE;
  135.         demomode = FALSE;
  136.         graphics = FALSE;
  137.      }
  138.      if (argprocessed == FALSE)
  139.      {
  140.         printf("Error: Invalid parameter %s", argv[ai]);
  141.         exit(1);
  142.      }
  143.      argprocessed = FALSE;
  144.       }
  145.    }
  146. #endif
  147.  
  148. #ifdef STANDALONE
  149.    if (graphics)
  150.    {
  151.       // Install the Super VGA 256 color driver
  152.       installuserdriver("Svga256", Svga256Detect);
  153.  
  154.       // read the result of installation
  155.       errorcode = graphresult();
  156.  
  157.       if (errorcode != grOk)  // an error occurred
  158.       {
  159.      printf("Graphics error: %s\n", grapherrormsg(errorcode));
  160.      printf("Press any key to quit...");
  161.      getch();
  162.      exit(1);
  163.       }
  164.  
  165.       // Initalize graphics mode
  166.       initgraph(&gd, &gm, "");
  167.  
  168.       // read result of initialization
  169.       errorcode = graphresult();
  170.  
  171.       if (errorcode != grOk)  // an error occurred
  172.       {
  173.      printf("Graphics error: %s\n", grapherrormsg(errorcode));
  174.      printf("Press any key to quit...");
  175.      getch();
  176.      exit(1);
  177.       }
  178.  
  179.       // Load the pallette
  180.       loadpal("std.pal");
  181.    }
  182. #else
  183.    if (graphics)
  184.       cleardevice();
  185. #endif
  186.  
  187.    // If we should write to files, open the files
  188.    if (wtof)
  189.    {
  190.       tmf = fopen("totalmov.eqs", "wt");
  191.       mmf = fopen("maxmov.eqs", "wt");
  192.       ef = fopen("energy.eqs", "wt");
  193.    }
  194.  
  195.    if (wtof && (!tmf || !mmf || !ef))
  196.    {
  197.       printf("Error: Could not create data files");
  198.       exit(1);
  199.    }
  200.  
  201.    // Set up the TEX header
  202.    if (wtof)
  203.    {
  204.      fprintf(tmf,"\\settabs 4 \\columns\n\n");
  205.      fprintf(tmf,"\\+Intensity&No Control&Active Control&Hybrid Control\\cr\n");
  206.      fprintf(mmf,"\\settabs 4 \\columns\n\n");
  207.      fprintf(mmf,"\\+Intensity&No Control&Active Control&Hybrid Control\\cr\n");
  208.      fprintf(ef, "\\settabs 4 \\columns\n\n");
  209.       fprintf(ef,"\\+Intensity&No Control&Active Control&Hybrid Control\\cr\n");
  210.    }
  211.  
  212.    if (graphics)
  213.    {
  214.       // Initialize the polygon's coordinates
  215.       bpoly[0] = 300;
  216.       bpoly[1] = 249;
  217.  
  218.       bpoly[2] = 340;
  219.       bpoly[3] = 249;
  220.  
  221.       bpoly[5] = 200;
  222.       bpoly[7] = 200;
  223.  
  224.       bpoly[8] = bpoly[0];
  225.       bpoly[9] = bpoly[1];
  226.    }
  227.  
  228.    for (i=TSTEP; i<9.5; i+=TSTEP)
  229.    {
  230. NewIntens:
  231.       // Write the intensity to the data files:
  232.  
  233.      if (wtof)
  234.      {
  235.         fprintf(tmf, "\\+%.1f&", i);
  236.         fprintf(mmf, "\\+%.1f&", i);
  237.         fprintf(ef, "\\+%.1f&", i);
  238.      }
  239.  
  240.       // No Control
  241.       intensity = pow(10, i/3);
  242.       init_qglobals(intensity, FELASTIC, FFRICTION, TSTEP, SNUM, BMASS);
  243.       if (!graphics)
  244.       {
  245.          show_control_txt(no_cntrl, "No Control:",i);
  246.       }
  247.       if (graphics)
  248.       {
  249.         ti = show_control(no_cntrl, "No Control:",i);
  250.         if (ti == EXIT)
  251.            goto Done;
  252.         if (ti != 0.0)
  253.         {
  254.            done_qglobals();
  255.            i = ti;
  256.            loadpal("std.pal");
  257.            goto NewIntens;
  258.         }
  259.       }
  260.       done_gen_qglobals();
  261.  
  262.       // Show Active Control
  263.       init_gen_qglobals(FELASTIC, FFRICTION, TSTEP, SNUM, BMASS);
  264.       if (!graphics)
  265.       {
  266.          show_control_txt(active_cntrl, "Active Control:",i);
  267.       }
  268.       if (graphics)
  269.       {
  270.         ti = show_control(active_cntrl, "Active Control:",i);
  271.         if (ti == EXIT)
  272.            goto Done;
  273.         if (ti != 0.0)
  274.         {
  275.            done_qglobals();
  276.            i = ti;
  277.            loadpal("std.pal");
  278.            goto NewIntens;
  279.         }
  280.       }
  281.       done_gen_qglobals();
  282.  
  283.       // Show Hybrid Control
  284.       init_gen_qglobals(FELASTIC, FFRICTION, TSTEP, SNUM, BMASS);
  285.       if (!graphics)
  286.       {
  287.          show_control_txt(hybrid_cntrl, "Hybrid Control:",i);
  288.       }
  289.       if (graphics)
  290.       {
  291.         ti = show_control(hybrid_cntrl, "Hybrid Control:",i);
  292.         if (ti == EXIT)
  293.            goto Done;
  294.         if (ti != 0.0)
  295.         {
  296.            done_qglobals();
  297.            i = ti;
  298.            loadpal("std.pal");
  299.            goto NewIntens;
  300.         }
  301.       }
  302.       if (wtof)
  303.       {
  304.         fprintf(tmf, "\\cr\n");
  305.         fprintf(mmf, "\\cr\n");
  306.         fprintf(ef, "\\cr\n");
  307.       }
  308.       done_qglobals();
  309.    }
  310.  
  311.    if (wtof)
  312.    {
  313.       fclose(tmf);
  314.       fclose(mmf);
  315.       fclose(ef);
  316.    }
  317. Done:
  318. #ifdef STANDALONE
  319.    closegraph();
  320. #endif
  321. }
  322.  
  323. #ifdef STANDALONE
  324. // Returns what video mode to use.
  325. //
  326. int huge Svga256Detect(void)
  327. {
  328.    return SVGA640x480x256;
  329. }
  330. #endif
  331.  
  332. // The function for no control
  333. //
  334. // Input: Time
  335. //
  336. // Output: 0
  337. //
  338. double no_cntrl(double t)
  339. {
  340.    return 0.0;
  341. }
  342.  
  343. // The active control function.
  344. //
  345. // Input: Time
  346. //
  347. // Output: The number to add to the force
  348. //
  349. // The algorithm used is:
  350. //
  351. // f        = -hx(t - T)
  352. //  control
  353. //
  354. // where h is a coefficient, t is the current time, and T depends on the
  355. // delay of the control system.
  356. //
  357. double active_cntrl(double t)
  358. {
  359.    double r = (-CONTROL_NORMAL * blmov[(int)(t / tstep) - DELAY].x);
  360.  
  361.    blmov[(int)(t / tstep)].e = r * (blmov[(int)(t / tstep)].x - blmov[(int)
  362.                  (t / tstep) - 1].x);
  363.    return r;
  364. }
  365.  
  366. // The hybrid control function.
  367. //
  368. // Input: Time
  369. //
  370. // Output: The number to add to the force
  371. //
  372. // The algorithm used is:
  373. //
  374. // if x(t - T) <= p then:
  375. //
  376. // f        = 0.0
  377. //  control
  378. //
  379. // else
  380. //
  381. // f        = -hx(t)
  382. //  control
  383. //
  384. // where h is a coefficient, t is the current time,
  385. // and T depends on the delay of the control system.
  386. //
  387. double hybrid_cntrl(double t)
  388. {
  389.    double r = 0.0;
  390.  
  391.    blmov[(int)(t/tstep)].e = 0.0;
  392.    if (blmov[(int)(t / tstep) - DELAY].x > HYBRID_SPOINT)
  393.       r = (-CONTROL_NORMAL * blmov[(int)(t / tstep)].x);
  394.    return r;
  395. }
  396.  
  397. // Executes the control functions if the array element is greater than
  398. // DELAY-1.
  399. //
  400. double execute_cntrl(cntrlfunc cf, double param)
  401. {
  402.    if ((int)(param / tstep) - DELAY < 0)
  403.    {
  404.       blmov[(int)(param/tstep)].e = 0.0;
  405.       return 0.0;
  406.    }
  407.    return cf(param);
  408. }
  409.  
  410. // Initializes the earthquake parameters.
  411. //
  412. // Input: Intensity, Elastic force coefficient, Friction force coefficient,
  413. // the time step, the number of steps, and the building's mass.
  414. //
  415. // Output: None
  416. //
  417. void init_qglobals(double intens, double fe_cos, double ff_cos, double t_step,
  418.      int s_num, double b_mass)
  419. {
  420.    snum = s_num;
  421.    if (blmov)
  422.       done_qglobals();
  423.    blmov = (bms far *)farcalloc(snum, sizeof(bms));
  424.    if (!blmov)
  425.    {
  426.       closegraph();
  427.       printf("Not enough memory\n");
  428.       exit(1);
  429.    }
  430.    for (int j=0; j<snum; j++)
  431.    {
  432.       blmov[j].x = 0.0; blmov[j].v = 0.0; blmov[j].f = 0.0; blmov[j].a = 0.0;
  433.       blmov[j].e = 0.0;
  434.    }
  435.    tstep = t_step;
  436.    felastic_cos = fe_cos;
  437.    ffriction_cos = ff_cos;
  438.    bmass = b_mass;
  439.    init_globals(intens, WAVELET_NUM, tstep * snum, DELTA_OMEGA, FRACTAL_DIM);
  440. }
  441.  
  442. // Initializes the building movement parameters without initializing the
  443. // earthquake parameters.
  444. //
  445. // Input: Elastic force coefficient, Friction force coefficient,
  446. // the time step, the number of steps, and the building's mass.
  447. //
  448. // Output: None
  449. //
  450. void init_gen_qglobals(double fe_cos, double ff_cos, double t_step,
  451.      int s_num, double b_mass)
  452. {
  453.    snum = s_num;
  454.    if (blmov)
  455.       done_gen_qglobals();
  456.    blmov = (bms far *)farcalloc(snum, sizeof(bms));
  457.    if (!blmov)
  458.    {
  459.       closegraph();
  460.       printf("Not enough memory\n");
  461.       exit(1);
  462.    }
  463.    for (int j=0; j<snum; j++)
  464.    {
  465.       blmov[j].x = 0.0; blmov[j].v = 0.0; blmov[j].f = 0.0; blmov[j].a = 0.0;
  466.       blmov[j].e = 0.0;
  467.    }
  468.    tstep = t_step;
  469.    felastic_cos = fe_cos;
  470.    ffriction_cos = ff_cos;
  471.    bmass = b_mass;
  472. }
  473.  
  474. // Destroys the global variables
  475. //
  476. void done_qglobals(void)
  477. {
  478.    farfree(blmov);
  479.    blmov = NULL;
  480.    done_globals();
  481. }
  482.  
  483. // Destroys the global variables without destroying the earthquake parameters.
  484. //
  485. void done_gen_qglobals(void)
  486. {
  487.    farfree(blmov);
  488.    blmov = NULL;
  489. }
  490.  
  491. // Generates the building movement
  492. //
  493. // Input: A control function
  494. //
  495. // Output: None
  496. //
  497. // The following algorithm is used for every step where t is the current time.
  498. //
  499. // x(t) = v(t - s) * s + x(t - s)
  500. //
  501. // v(t) = a(t - s) * s + v(t - s)
  502. //
  503. // f(t) = f         + f        + f         + f
  504. //         external    elastic    friction    control
  505. //
  506. // f        = -kx(t)
  507. //  elastic
  508. //
  509. // f         = -nv(t)
  510. //  friction
  511. //
  512. // a(t) = f(t)
  513. //        ----
  514. //         m
  515. //
  516. // e(t) = f        * x(t) - x(t - s)
  517. //         control
  518. //  /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\
  519. // for active control and 0 for all others.
  520. //
  521. // where s is the time step, m is the mass of the building, k and n are
  522. // coefficients, f(t) is the force in a current time, x(t) is the position
  523. // related to the original position at the current time, v(t) is the velocity
  524. // at the current time, and a(t) is the accelaration at the current time.
  525. //
  526. void gen_blmov(cntrlfunc cf)
  527. {
  528.    // Generate values for time 0
  529.    blmov[0].x = 0.0;
  530.    blmov[0].v = 0.0;
  531.    blmov[0].f = disp(0.0) + execute_cntrl(cf, 0.0);
  532.    blmov[0].a = blmov[0].f / bmass;
  533.    // Generate all of the other values
  534.    for (int j=1; j<snum; j++)
  535.    {
  536.       blmov[j].x = blmov[j-1].v * tstep + blmov[j-1].x;
  537.       blmov[j].v = blmov[j-1].a * tstep + blmov[j-1].v;
  538.       blmov[j].f = disp(j * tstep) + (-felastic_cos * blmov[j-1].x) +
  539.                       (-ffriction_cos * blmov[j-1].v) +
  540.                       execute_cntrl(cf, j * tstep);
  541.       blmov[j].a = blmov[j].f / bmass;
  542.     }
  543. }
  544.  
  545. // Plots the building movement.
  546. //
  547. void plot_blmov(int x, int y, int x1, int y1, int borcolor, int normcolor,
  548.            int limitcolor)
  549. {
  550.    int cx, cy, middle, color;
  551.    int limit;
  552.  
  553.    setcolor(borcolor);
  554.    rectangle(x, y, x1, y1);       // Draw border rectangle
  555.    middle = ((y1-y) / 2) + y;
  556.    limit = x1+1;
  557.    if ((x1+1)-x > snum)
  558.       limit = x+snum;
  559.    for (cx=x;cx<limit;cx++)
  560.    {
  561.       cy = (int)blmov[cx-x].x * MAGNIFICATION + middle;
  562.       color = normcolor;
  563.       if (cy < y)
  564.       {
  565.      cy = y;
  566.      color = limitcolor;
  567.       }
  568.       if (cy > y1)
  569.       {
  570.      cy = y1;
  571.      color = limitcolor;
  572.       }
  573.       putpixel(cx, cy, color);
  574.    }
  575. }
  576.  
  577. // Plots the earthquake displacements
  578. //
  579. void plot_disp(int x, int y, int x1, int y1, int borcolor, int normcolor,
  580.            int limitcolor)
  581. {
  582.    int cx, cy, middle, color;
  583.    int limit;
  584.  
  585.    setcolor(borcolor);
  586.    rectangle(x, y, x1, y1);       // Draw border rectangle
  587.    middle = ((y1-y) / 2) + y;
  588.    limit = x1+1;
  589.    if (x1-x > snum)
  590.       limit = x+snum;
  591.    for (cx=x;cx<limit;cx++)
  592.    {
  593.       cy = (int)disp((cx - x) * tstep) + middle;
  594.       color = normcolor;
  595.       if (cy < y)
  596.       {
  597.      cy = y;
  598.      color = limitcolor;
  599.       }
  600.       if (cy > y1)
  601.       {
  602.      cy = y1;
  603.      color = limitcolor;
  604.       }
  605.       putpixel(cx, cy, color);
  606.    }
  607. }
  608.  
  609. #ifdef STANDALONE
  610. // Loads the palette from a raw palette file.
  611. //
  612. void loadpal(char *fn)
  613. {
  614.    int color;
  615.    int r, g, b;
  616.    FILE *pal;
  617.  
  618.    pal = fopen(fn, "rb");
  619.  
  620.    if (pal == NULL)
  621.    {
  622.       closegraph();
  623.       printf("Error: Could not open palette file %s", fn);
  624.       exit(1);
  625.    }
  626.    for (color = 0; color < 256; color++)
  627.    {
  628.       fread(&r, 1, 1, pal);
  629.       fread(&g, 1, 1, pal);
  630.       fread(&b, 1, 1, pal);
  631.       setrgbpalette(color, r, g, b);
  632.    }
  633.    fclose(pal);
  634. };
  635. #endif
  636.  
  637. // Calculates the total building movements
  638. //
  639. double total_blmov(void)
  640. {
  641.    double sum = 0.0;
  642.  
  643.    for (int j=0; j<snum; j++)
  644.       sum += fabs(blmov[j].x);
  645.  
  646.    return sum;
  647. }
  648.  
  649. // Returns the maximum building movement.
  650. //
  651. double max_blmov(void)
  652. {
  653.    double max;
  654.  
  655.    if (snum == 1)
  656.      return fabs((double)blmov[0].x);
  657.    max = (fabs(blmov[0].x) > fabs(blmov[1].x)) ?
  658.                          fabs(blmov[0].x)
  659.                           : fabs(blmov[1].x);
  660.    if (snum == 2)
  661.       return max;
  662.    for (int j=2; j<snum; j++)
  663.      max = (max > fabs(blmov[j].x)) ? max : fabs(blmov[j].x);
  664.    return max;
  665. }
  666.  
  667. // Returns the total energy used.
  668. //
  669. double total_eused(void)
  670. {
  671.    double sum = 0.0;
  672.  
  673.    for (int j=0; j<snum; j++)
  674.       sum += fabs(blmov[j].e);
  675.  
  676.    return sum;
  677. }
  678.  
  679. // Shows the control wave and the earthquake wave.
  680. // Returns 0.0 or to what number to change the current intensity.
  681. //
  682. double show_control(cntrlfunc cf, char *cfname, double intens)
  683. {
  684.    char tmp[20];
  685.    char ch;
  686.    int y;
  687.  
  688.    // Set the colors
  689.    setfillstyle(SOLID_FILL, BLUE);
  690.    bar(1,1,640,300);
  691.  
  692.    setfillstyle(SOLID_FILL, 79);
  693.    bar(1,250,640,300);
  694.  
  695.    // If in demo mode, write the word DEMO
  696.    if (demomode)
  697.    {
  698.       sprintf(tmp, "DEMO");
  699.       settextstyle(TRIPLEX_FONT, HORIZ_DIR, 8);
  700.       setcolor(32);
  701.       outtextxy(300, 10, tmp);
  702.       setcolor(WHITE);
  703.       settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  704.    }
  705.  
  706.    // Show the intensity
  707.    sprintf(tmp, "Intensity: %.1lf", intens);
  708.      outtextxy(10, 10, tmp);
  709.    y = 10 + textheight(tmp) + 2;
  710.  
  711.    // Show the earthquake displacement
  712.    sprintf(tmp, "Earthquake Disp:");
  713.      outtextxy(130, 300, tmp);
  714.    plot_disp(10,310,310,479,WHITE,WHITE,RED);
  715.  
  716.    // Show the building movement
  717.    gen_blmov(cf);
  718.    outtextxy(440, 300, cfname);
  719.  
  720.    plot_blmov(320,310,620,479,WHITE,WHITE,RED);
  721.  
  722.    // Display the data
  723.    sprintf(tmp, "Total: %.3lf", total_blmov());
  724.      outtextxy(10,y,tmp);
  725.  
  726.    if (wtof)
  727.       fprintf(tmf, "%.3lf&", total_blmov());
  728.  
  729.    y += textheight(tmp);
  730.    sprintf(tmp, "Max. Move.: %.3lf", max_blmov());
  731.      outtextxy(10,y,tmp);
  732.  
  733.    if (wtof)
  734.       fprintf(mmf, "%.3lf&", max_blmov());
  735.  
  736.    y += textheight(tmp);
  737.    sprintf(tmp, "E. Used: %.3lf", total_eused());
  738.      outtextxy(10,y,tmp);
  739.  
  740.    if (wtof)
  741.       fprintf(ef, "%.3lf&", total_eused());
  742.  
  743.    y += textheight(tmp);
  744.  
  745.    bpoly[4] = 340;
  746.    bpoly[6] = 300;
  747.  
  748.    setfillstyle(SOLID_FILL, 128);
  749.    fillpoly(5, bpoly);
  750.    setfillpattern(bpattern, WHITE);
  751.    fillpoly(5, bpoly);
  752.  
  753.    setfillstyle(SOLID_FILL, BLUE);
  754.  
  755.    for (int k=0; k<SNUM; k++)
  756.    {
  757.       if (bpoly[4] != (int)blmov[k].x + 340 && bpoly[6] !=
  758.        (int)blmov[k].x + 300)
  759.       {
  760.      bar(1,y+1,640, 249);
  761.      bpoly[4] = (int)blmov[k].x + 340;
  762.      bpoly[6] = (int)blmov[k].x + 300;
  763.  
  764.      setfillstyle(SOLID_FILL, 128);
  765.      fillpoly(5, bpoly);
  766.      setfillpattern(bpattern, WHITE);
  767.      fillpoly(5, bpoly);
  768.      setfillstyle(SOLID_FILL, 124);
  769.  
  770.      setfillstyle(SOLID_FILL, BLUE);
  771.       }
  772.       else
  773.        delay(17);      // If no time taken to draw delay for 17 ms
  774.    }
  775.  
  776.    if (!demomode)
  777.    {
  778. #ifdef STANDALONE
  779.       outtextxy(200, 265, "Press any key to continue...");
  780.       outtextxy(200, 275, "To change intensities press <I>");
  781.       outtextxy(200, 285, "To exit press <ESC>");
  782. #else
  783.       outtextxy(200, 265, "Press any key to continue...");
  784.       outtextxy(200, 275, "To exit press <ESC>");
  785. #endif
  786.       while (!kbhit());
  787.       ch = getch();
  788.       setfillstyle(SOLID_FILL, BLACK);
  789.       bar(1,1,640,480);
  790. #ifdef STANDALONE
  791.       if (ch == 'i' || ch == 'I')
  792.      return select_intensity();
  793. #endif
  794.       if (ch == 27)
  795.       {
  796.      done_qglobals();
  797.      if (wtof)
  798.      {
  799.         fclose(tmf);
  800.         fclose(mmf);
  801.         fclose(ef);
  802.      }
  803. #ifdef STANDALONE
  804.      closegraph();
  805. #endif
  806.      return EXIT;
  807.        }
  808.    }
  809.  
  810.    if (demomode)
  811.    {
  812.       if (kbhit())
  813.       {
  814.      done_qglobals();
  815.      if (wtof)
  816.      {
  817.         fclose(tmf);
  818.         fclose(mmf);
  819.         fclose(ef);
  820.      }
  821. #ifdef STANDALONE
  822.      closegraph();
  823. #endif
  824.      return EXIT;
  825.       }
  826.       setfillstyle(SOLID_FILL, BLACK);
  827.       bar(1,1,640,480);
  828.    }
  829.    return 0.0;
  830. }
  831.  
  832. // Shows the current intensity, etc. and writes the data to a file.
  833. //
  834. void show_control_txt(cntrlfunc cf, char *cfname, double intens)
  835. {
  836.    char tmp[20];
  837.    char ch;
  838.  
  839.    // Show the intensity
  840.    sprintf(tmp, "Intensity: %.1lf", intens);
  841.    printf("%s", tmp);
  842.    printf("\n");
  843.  
  844.    printf("%s\n", cfname);
  845.  
  846.    cprintf("Please wait...");
  847.  
  848.    // Generate the building movement
  849.    gen_blmov(cf);
  850.  
  851.    // Display the data
  852.    sprintf(tmp, "Total: %.3lf", total_blmov());
  853.    printf("\n%s\n", tmp);
  854.  
  855.    if (wtof)
  856.       fprintf(tmf, "%.3lf&", total_blmov());
  857.  
  858.    sprintf(tmp, "Max. Move.: %.3lf", max_blmov());
  859.    printf("%s\n", tmp);
  860.  
  861.    if (wtof)
  862.       fprintf(mmf, "%.3lf&", max_blmov());
  863.  
  864.    sprintf(tmp, "E. Used: %.3lf", total_eused());
  865.    printf("%s\n\n", tmp);
  866.  
  867.    if (wtof)
  868.       fprintf(ef, "%.3lf&", total_eused());
  869.  
  870.    if (kbhit())
  871.    {
  872.       ch = getch();
  873.       done_qglobals();
  874.       if (wtof)
  875.       {
  876.      fclose(tmf);
  877.      fclose(mmf);
  878.      fclose(ef);
  879.       }
  880.       exit(0);
  881.    }
  882. }
  883.  
  884. #ifdef STANDALONE
  885. // Asks the user for a new intensity.
  886. //
  887. double select_intensity(void)
  888. {
  889.    double r;
  890.    int gd = DETECT, gm;
  891.  
  892.    restorecrtmode();
  893.    printf("What intensity do you want to switch to (X.X)? ");
  894.    scanf("%3lf", &r);
  895.    if (r > 9.5)
  896.      r = 9.5;
  897.    setgraphmode(getgraphmode());
  898.    return (r);
  899. }
  900. #endif
  901.